<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">{{i18n.address_title}}</text>
			<text @click="chooseLocation" class="input">
				{{addressData.address}} {{addressData.name}}
			</text>
			<text class="yticon icon-shouhuodizhi"></text>
		</view>
		<view class="row b-b"> 
			<text class="tit">{{i18n.loc_detail_title}}</text>
			<input class="input" type="text" v-model="addressData.describe" :placeholder="this.$lang.address_addressManage.loc_ph_title" placeholder-class="placeholder" />
		</view>
		
		<view class="row default-row">
			<text class="tit">{{i18n.set_default_title}}</text>
			<switch :checked="addressData.is_default" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">{{i18n.submit_title}}</button>
	</view>
</template>

<script>
	import request_async from '../../common/request_async.js';
	export default {
		data() {
			return {
				addressData: {
					address: this.$lang.address_addressManage.select_title,
					name: '',
					describe: '',
					is_default: false,
					longitude: '',
					latitude: '',
				}
			}
		},
		onLoad(option){
			let title = this.$lang.address_addressManage.add_address_title;
			if(option.type==='edit'){
				title = this.$lang.address_addressManage.edit_address_title;
				this.addressData = JSON.parse(option.data);
				if(this.addressData.is_default == 1){
					this.addressData.is_default = true;
				}else{
					this.addressData.is_default = false;
				}
				console.log(this.addressData)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		onShow() {
			uni.setNavigationBarTitle({
			    title: this.$lang.address_addressManage.title
			});
		},
		computed: {
			i18n () {  
			  return this.$lang.address_addressManage  
			}  
		},
		methods: {
			switchChange(e){
				this.addressData.is_default = e.detail.value;
				// console.log(e.detail);
			},
			
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						console.log(data);
						console.log('位置名称：' + data.name);
						console.log('详细地址：' + data.address);
						console.log('纬度：' + data.latitude);
						console.log('经度：' + data.longitude);
						this.addressData.latitude = data.latitude;
						this.addressData.longitude = data.longitude;
						this.addressData.name = data.name;
						this.addressData.address = data.address;
					}
				})
			},
			
			//提交
			async confirm(){
				let data = this.addressData;
				if(!data.address){
					this.$api.msg(this.$lang.address_addressManage.select_map_title);
					return;
				}
				if(!data.describe){
					this.$api.msg(this.$lang.address_addressManage.input_loc_title);
					return;
				}
				var post_data = {name: data.name, address: data.address, describe: data.describe, latitude: data.latitude, longitude: data.longitude};
				if(data.id){
					post_data.id = data.id;
				}
				post_data.is_default = data.is_default ? 1 : 0;
				console.log('confirm post: ', post_data);
				var res = await request_async({
					name: 'user.address.add',
					method: 'POST',
					data: post_data,
				})
				//this.$api.prePage()获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				// this.$api.prePage().refreshList(data, this.manageType);
				if(res.code == 0){
					this.$api.msg(this.manageType=='edit' ? this.$lang.address_addressManage.modify_succ_title : this.$lang.address_addressManage.add_succ_title);
				}else{
					this.$api.msg(res.msg);
				}
				
				setTimeout(()=>{
					// uni.navigateBack()
					uni.navigateTo({
						url: `/pages/address/address`
					})
				}, 800)
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
